import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { MonitorChartComponent } from "../../../../shared-component/monitor-chart/monitor-chart.component";
import * as differenceInCalendarDays from 'date-fns/difference_in_calendar_days';
import set = Reflect.set;

@Component({
  selector: 'app-function-monitor',
  templateUrl: './function-monitor.component.html',
  styleUrls: ['./function-monitor.component.less']
})
export class FunctionMonitorComponent implements OnInit {
  @ViewChild("invocationChart", { static: true }) invocationChart: MonitorChartComponent;
  @ViewChild('failureChart', { static: true }) failureChart: MonitorChartComponent;
  @ViewChild('executeChart', { static: true }) executeChart: MonitorChartComponent;
  @ViewChild('memoryChart', { static: true }) memoryChart: MonitorChartComponent;
  @Input() groupId = '';
  @Input() functionId = '';
  @Input() versionName = '';
  timeRangeHour = '1';
  startTime;
  endTime;
  stepMultiple = 1; // 数据采样间隔
  time;
  setIntervalId;
  openRefresh: boolean = false;
  constructor() { }

  ngOnInit() {
    this.getMonitors(this.timeRangeHour);
  }

  ngOnDestroy() {
    if (this.setIntervalId) {
      clearInterval(this.setIntervalId);
    }
  }

  disabledDate = (current: Date): boolean => {
    const disabled = differenceInCalendarDays(current, new Date()) > 0 || Math.abs(differenceInCalendarDays(current, new Date())) > 59;
    return disabled;
  };

  changeDate() {
    this.timeRangeHour = '';
    this.getPeriod();
    this.getMonitors();
    this.setIntervalRefresh();
  }

  getPeriod() {
    let divideData = Math.abs(this.time[0] - this.time[1]) / 1000 / 60;
    if (divideData <= 60) {
      this.stepMultiple = 1;
    } else if (divideData > 60 && divideData <= 1440) {
      this.stepMultiple = 5;
    } else if (divideData > 1440) {
      this.stepMultiple = 60;
    }
  }

  handleTime() {
    if (this.time && this.time.length > 0) {
      this.startTime = Math.floor(this.time[0].getTime() / 1000);
      this.endTime = Math.floor(this.time[1].getTime() / 1000);
    }
  }

  timeChange() {
    // console.log(this.time)
    if (this.time.length <= 0) {
      this.timeRangeHour = '1';
      this.getMonitors();
    } else {
      this.timeRangeHour = '';
    }
  }

  refresh() {
    this.time = undefined;
    this.startTime = undefined;
    this.endTime = undefined;
    this.timeRangeHour = '1';
    this.getMonitors(this.timeRangeHour);
  }

  /**
   * 切换时间段刷新图表
   * @param value
   */
  getMonitors(value: string = this.timeRangeHour) {
    this.handleOtherTime(value);
  }

  handleOtherTime(value: string = this.timeRangeHour) {
    if (value == "1") {
      this.stepMultiple = 1;
    }
    else if (value == "24") {
      this.stepMultiple = 5;
    }
    else if (value == "168") {
      this.stepMultiple = 60;
    }
    if (this.timeRangeHour != '') {
      this.time = null;
      let nowDate = new Date();
      nowDate.setSeconds(0);
      this.endTime = Math.floor(nowDate.getTime() / 1000);
      let nowDate2 = new Date();
      nowDate2.setSeconds(0);
      nowDate2.setHours(nowDate.getHours() - parseInt(value));
      this.startTime = Math.floor(nowDate2.getTime() / 1000);
    } else {
      this.handleTime();
    }
  }

  getSetIntervalMonitor() {
    this.handleOtherTime();
    this.invocationChart.ngOnChanges(undefined);
    this.failureChart.ngOnChanges(undefined);
    this.executeChart.ngOnChanges(undefined);
    this.memoryChart.ngOnChanges(undefined);
  }

  openRefreshChange() {
    if (this.openRefresh) {
      this.getSetIntervalMonitor();
    }
    this.setIntervalRefresh();
  }

  // 定时请求
  setIntervalRefresh() {
    if (this.openRefresh) {
      if (this.setIntervalId) {
        clearInterval(this.setIntervalId);
      }
      this.setIntervalId = setInterval(() => {
        this.getSetIntervalMonitor();
      }, 60000);
    } else {
      clearInterval(this.setIntervalId);
    }
  }

}
